<template>
  <a-modal
    :title="title"
    :visible="visible"
    :confirm-loading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    destroyOnClose>
    <a-form-model
      ref="ruleForm"
      :model="formData"
      :rules="rules"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 20 }">
      <a-form-model-item label="核销码" prop="orderCode">
        <a-input
          :maxLength="60"
          allow-clear
          v-model="formData.orderCode"
          placeholder="请输入核销码" />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
export default {
  props: {
    value: {
      require: true,
      default: false,
      type: Boolean,
    },
    title: {
      default: "自提核销",
      type: String,
    },
    records: {
      type: Object,
      default() {
        return {};
      },
    }
  },
  data() {
    return {
      visible: false,
      confirmLoading: false,
      formData: {
        orderGoodsId: undefined,
        orderCode: undefined
      },
      rules: {
        orderCode: [{ required: true, message: '请输入核销码', trigger: ['change', 'blur'] }]
      },
      dataInfo: {}
    };
  },
  watch: {
    value(val) {
      this.visible = val;
    },
    visible(val) {
      if (val && this.records.id) {
        this.formData = {
          orderGoodsId: undefined,
          orderCode: undefined
        }
      }
    },
  },
  created() {},
  mounted() {},
  methods: {
    handleOk() {
      this.confirmLoading = true
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.formData.orderGoodsId = this.records.id
          this.$post('/order/goods/cancel/verification', this.formData).then((res) => {
            this.confirmLoading = false
            console.log(res)
            if (res && res.returnCode === 1000) {
              this.$message.success("核销成功！");
              this.$emit("handleSubmit");
              this.formData = {
                orderGoodsId: '',
                orderCode: ''
              }
            }
          })
          .catch(err => {
            this.confirmLoading = false
          });
        } else {
          this.confirmLoading = false
          this.$message.error("请输入核销码");
        }
      })
      
    },
    handleCancel(e) {
      this.$emit("handleCancel");
      this.visible = false;
    },
  }
};
</script>

<style lang="less" scoped></style>
